#include "uart_protocol.h"
#include <string.h>

// 协议解析器初始化
void protocol_init(protocol_parser_t *parser)
{
    parser->state = PARSE_STATE_HEADER_H;
    parser->data_index = 0;
    parser->expected_length = 0;
    parser->calculated_checksum = 0;
    memset(&parser->frame, 0, sizeof(protocol_frame_t));
}

// 计算校验和
uint8_t protocol_calculate_checksum(uint8_t length, uint8_t command, uint8_t *data)
{
    uint8_t checksum = length ^ command;
    for (uint8_t i = 0; i < length; i++) {
        checksum ^= data[i];
    }
    return checksum;
}

// 验证帧的完整性
bool protocol_validate_frame(protocol_frame_t *frame)
{
    // 检查帧头
    if (frame->header[0] != PROTOCOL_HEADER_H || frame->header[1] != PROTOCOL_HEADER_L) {
        return false;
    }
    
    // 检查帧尾
    if (frame->tail[0] != PROTOCOL_TAIL_H || frame->tail[1] != PROTOCOL_TAIL_L) {
        return false;
    }
    
    // 检查校验和
    uint8_t calculated_checksum = protocol_calculate_checksum(frame->length, frame->command, frame->data);
    if (frame->checksum != calculated_checksum) {
        return false;
    }
    
    return true;
}

// 逐字节解析协议
bool protocol_parse_byte(protocol_parser_t *parser, uint8_t byte)
{
    switch (parser->state) {
        case PARSE_STATE_HEADER_H:
            if (byte == PROTOCOL_HEADER_H) {
                parser->frame.header[0] = byte;
                parser->state = PARSE_STATE_HEADER_L;
            }
            break;
            
        case PARSE_STATE_HEADER_L:
            if (byte == PROTOCOL_HEADER_L) {
                parser->frame.header[1] = byte;
                parser->state = PARSE_STATE_LENGTH;
            } else {
                parser->state = PARSE_STATE_HEADER_H;
            }
            break;
            
        case PARSE_STATE_LENGTH:
            parser->frame.length = byte;
            parser->expected_length = byte;
            parser->data_index = 0;
            parser->calculated_checksum = byte;
            parser->state = PARSE_STATE_COMMAND;
            break;
            
        case PARSE_STATE_COMMAND:
            parser->frame.command = byte;
            parser->calculated_checksum ^= byte;
            if (parser->expected_length == 0) {
                parser->state = PARSE_STATE_CHECKSUM;
            } else {
                parser->state = PARSE_STATE_DATA;
            }
            break;
            
        case PARSE_STATE_DATA:
            parser->frame.data[parser->data_index] = byte;
            parser->calculated_checksum ^= byte;
            parser->data_index++;
            if (parser->data_index >= parser->expected_length) {
                parser->state = PARSE_STATE_CHECKSUM;
            }
            break;
            
        case PARSE_STATE_CHECKSUM:
            parser->frame.checksum = byte;
            parser->state = PARSE_STATE_TAIL_H;
            break;
            
        case PARSE_STATE_TAIL_H:
            if (byte == PROTOCOL_TAIL_H) {
                parser->frame.tail[0] = byte;
                parser->state = PARSE_STATE_TAIL_L;
            } else {
                parser->state = PARSE_STATE_HEADER_H;
            }
            break;
            
        case PARSE_STATE_TAIL_L:
            if (byte == PROTOCOL_TAIL_L) {
                parser->frame.tail[1] = byte;
                parser->state = PARSE_STATE_COMPLETE;
                // 验证校验和
                if (parser->frame.checksum == parser->calculated_checksum) {
                    return true;  // 解析完成且校验正确
                }
            }
            parser->state = PARSE_STATE_HEADER_H;
            break;
            
        case PARSE_STATE_COMPLETE:
            parser->state = PARSE_STATE_HEADER_H;
            break;
    }
    
    return false;
}

// 构建协议帧
uint8_t protocol_build_frame(uint8_t command, uint8_t *data, uint8_t length, uint8_t *frame_buffer)
{
    uint8_t index = 0;
    
    // 帧头
    frame_buffer[index++] = PROTOCOL_HEADER_H;
    frame_buffer[index++] = PROTOCOL_HEADER_L;
    
    // 长度
    frame_buffer[index++] = length;
    
    // 命令字
    frame_buffer[index++] = command;
    
    // 数据
    for (uint8_t i = 0; i < length; i++) {
        frame_buffer[index++] = data[i];
    }
    
    // 校验和
    frame_buffer[index++] = protocol_calculate_checksum(length, command, data);
    
    // 帧尾
    frame_buffer[index++] = PROTOCOL_TAIL_H;
    frame_buffer[index++] = PROTOCOL_TAIL_L;
    
    return index;
}

// ESP32主机端API实现
void esp32_send_uv_control(uint8_t status)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = status;
    uint8_t frame_size = protocol_build_frame(CMD_UV_CONTROL, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_hydrogen_control(uint8_t status)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = status;
    uint8_t frame_size = protocol_build_frame(CMD_HYDROGEN_CONTROL, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_light_color(uint8_t color)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = color;
    uint8_t frame_size = protocol_build_frame(CMD_LIGHT_COLOR, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_light_mode(uint8_t mode)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = mode;
    uint8_t frame_size = protocol_build_frame(CMD_LIGHT_MODE, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_buzzer_control(uint8_t count)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = count;
    uint8_t frame_size = protocol_build_frame(CMD_BUZZER_CONTROL, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_clean_control(uint8_t status)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = status;
    uint8_t frame_size = protocol_build_frame(CMD_CLEAN_CONTROL, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_shutdown(void)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = 0x00;
    uint8_t frame_size = protocol_build_frame(CMD_SHUTDOWN, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_backlight_control(uint8_t status)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = status;
    uint8_t frame_size = protocol_build_frame(CMD_BACKLIGHT_CONTROL, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void esp32_send_heartbeat(void)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = 0x00;
    uint8_t frame_size = protocol_build_frame(CMD_HEARTBEAT, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

// PY32从机端API实现
void py32_send_charge_status(uint8_t status)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = status;
    uint8_t frame_size = protocol_build_frame(CMD_CHARGE_STATUS, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
}

void py32_send_heartbeat(void)
{
    uint8_t frame_buffer[PROTOCOL_MAX_FRAME_SIZE];
    uint8_t data = 0x00;
    uint8_t frame_size = protocol_build_frame(CMD_HEARTBEAT, &data, 1, frame_buffer);
    uart_send_data(frame_buffer, frame_size);
} 